package com.fancy.service;

import com.fancy.dao.RelationShipFriendsDao;
import com.fancy.dao.StudentDao;
import com.fancy.dao.TeacherDao;
import entity.Friend;
import entity.RelationShip;
import entity.Student;
import entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jackie on 2017/10/21.
 */
@Component
public class RelationFriendsService {

    @Autowired
    private RelationShipFriendsDao relationShipFriendsDao;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private TeacherDao teacherDao;

    private PersonConvert personConvert = new PersonConvert();

    public int getMyFriendsCount(String myID){
        return relationShipFriendsDao.getMyFriendsCount(myID);
    }

    public List<Friend> getMyFriends(String myID){
        List<String> ids = relationShipFriendsDao.getFriendsID(myID);
        return personConvert.convertIDToFriends(ids);
    }

    private class PersonConvert{

        public List<Friend> convertIDToFriends(List<String> ids){

            List<Friend> friends = new ArrayList<Friend>();
            for (int i = 0 ; i < ids.size() ;  i ++) {
                String id = ids.get(i);
                Student student = studentDao.findStudent(id);
                Teacher teacher = teacherDao.findTeacher(id);
                if (student!=null){
                    friends.add(new Friend(student.student_id, student.name, student.sex, student.icon_location, student.motto));
                }else if (teacher!=null){
                    friends.add(new Friend(teacher.teacher_id, teacher.name, teacher.sex, teacher.icon_location, teacher.motto));
                }
            }

            return friends;
        }

    }

    public boolean addFriends(RelationShip relationShip) {
        Student mine = studentDao.findStudent(relationShip.person_1);
        if (mine != null) {
            Student student = studentDao.findStudent(relationShip.person_2);
            Teacher teacher = teacherDao.findTeacher(relationShip.person_2);
            if (student != null) {
                relationShipFriendsDao.insertRelationship(relationShip);
            }else if (teacher != null) {
                relationShipFriendsDao.insertRelationship(relationShip);
            }
        }
        return true;
    }

    public Friend findPerson(String friendID) {
        Student student = studentDao.findStudent(friendID);
        Teacher teacher = teacherDao.findTeacher(friendID);
        Friend friend = null;
        if (student != null) {
            friend = new Friend();
            friend.id = student.student_id;
            friend.name = student.name;
            friend.iconLocation = student.icon_location;
            friend.motto = student.motto;
            friend.sex = student.sex;
        }else if (teacher != null) {
            friend = new Friend();
            friend.id = teacher.teacher_id;
            friend.name = teacher.name;
            friend.iconLocation = teacher.icon_location;
            friend.sex = teacher.sex;
            friend.motto = teacher.motto;
        }
        return friend;
    }
}
